package com.frogs.ucenter.util.sms;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Connection.Method;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.frogs.framework.util.UrlUtil;
import com.frogs.framework.util.httpclient.ContentTypeEnum;
import com.frogs.framework.util.httpclient.HttpClientUtil;
import com.frogs.framework.util.httpclient.PostObject;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.entity.SmsMessageMqEntity;

/**
 * 蓝创短信接口
 * 联系人：谭朱林   13482416816  2880090336
 * 接口咨询人： 漆斌 18816550260 2880090297
 * @author hongyuhao
 *
 */
@Component
public class PartnerSmsLCHelper {

	private static Logger log = LoggerFactory.getLogger(PartnerSmsLCHelper.class);
	private static Map<String ,String> headMap= new HashMap<String,String>();
	
	private String sendUrl;
	private String account;
	private String pswd;
	private String content = "";
	private boolean needstatus = true;//是否需要状态报告，需要true，不需要false
	private String product = null;//产品ID
	private String extno = null;//扩展码
	
	static {
		headMap.put("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0;)" );
		headMap.put("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*");
		headMap.put("Bizmp-Version", "1.0");
	}
	
	public String getSendUrl() {
		return sendUrl;
	}

	public void setSendUrl(String sendUrl) {
		this.sendUrl = sendUrl;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPswd() {
		return pswd;
	}

	public void setPswd(String pswd) {
		this.pswd = pswd;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public boolean isNeedstatus() {
		return needstatus;
	}

	public void setNeedstatus(boolean needstatus) {
		this.needstatus = needstatus;
	}

	public String getProduct() {
		return product;
	}

	public void setProduct(String product) {
		this.product = product;
	}

	public String getExtno() {
		return extno;
	}

	public void setExtno(String extno) {
		this.extno = extno;
	}

	public String sendSms(SmsMessageMqEntity smsMessageMqEntity) {
		/**
		 * 用于把消息发送到短信平台
		 */
		/*
		 * http://222.73.117.158/msg/HttpBatchSendSM?account=jiekou-cs-01&pswd=Tch147256&mobile=13482416860&msg=【创蓝文化】您的注册验证码是：1131.请完成注册&needstatus=true
		 */
		String result = "";
		try {
			content = URLEncoder.encode(smsMessageMqEntity.getContent(),"UTF-8");;
		} catch (UnsupportedEncodingException e1) {
			log.error("func[sendSms] content["+smsMessageMqEntity.getContent()+"] exception[" + e1.getStackTrace() + "]");
			return "";
		}
		
		PostObject po = new PostObject();
		po.setCharset("UTF-8");
		po.setContentType(ContentTypeEnum.WWW_FORM.toString());
		po.setHeaders(headMap);
		po.setHttps(false);
		po.setUrl( this.sendUrl + "?account="+account+"&pswd="+pswd+"&mobile="+smsMessageMqEntity.getReceiver()+"&msg="+content+"&needstatus=true");
		result = String.valueOf(HttpClientUtil.get(po));
		return result;
	} 
}
